<template>
  <div class="register">
    <h1 class="title">注册</h1>
    <van-form>
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div>
        <van-button round block type="info" native-type="submit" @click.native="register">提交</van-button>
      </div>
    </van-form>
    <div class="login">
      <a href="javascript:;" @click="login">登录</a>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      username: "",
      password: ""
    };
  },
  methods: {
    login() {
      this.$router.push("/login");
    },
    register() {
      if (this.username && this.password) {
        this.$axios({
          url: "register",
          method: "post",
          data: {
            uname: this.username,
            password: this.password
          }
        }).then(res => {
          console.log(res);
          if (res.code == 0) {
            this.$dialog
              .confirm({
                message: res.msg,
                confirmButtonText: "重新注册",
                cancelButtonText: "去登录"
              })
              .then(() => {
                // on confirm
              })
              .catch(() => {
                // on cancel
                this.$router.push("/login");
              });
          }else if(res.code==1){
            this.$dialog
              .confirm({
                message: res.msg+'马上去登录'
              })
              .then(() => {
                // on confirm
                this.$router.push("/login");
              })
              .catch(() => {
                
                
              });
              
          }
        });
      }
    }
  }
};
</script>

<style lang="less" scoped>
.register {
  .title {
    text-align: center;
    font-weight: 400;
    padding: 10px;
  }
  .login {
    text-align: center;
    padding: 10px;
    a {
      color: royalblue;
      border-bottom: 1px solid royalblue;
    }
  }
  background-color: #fff;
  margin-top: 40%;
}
</style>